package com.xin.di.uav.applet.utils;

import com.aliyun.teaopenapi.models.Config;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Component;

import static com.aliyun.teautil.Common.toJSONString;

@Component
public class SendSmsUtils {
    public static Client createClient() {
        Config config = new Config()
                // 配置 AccessKey ID，请确保代码运行环境配置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("LTAI5tCpX6nRTEosndGR9CKx")
                // 配置 AccessKey Secret，请确保代码运行环境配置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("xXgpo2l3ocEVux5SwDEFSzPhJwr05D");
        // System.getenv()方法表示获取系统环境变量，不要直接在getenv()中填入AccessKey信息。

        // 配置 Endpoint。中国站使用dysmsapi.aliyuncs.com
        config.endpoint = "dysmsapi.aliyuncs.com";

        try {
            return new Client(config);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static SendSmsResponse sendSms(SendSmsRequest sendSmsRequest) {
        // 初始化请求客户端
        Client client = SendSmsUtils.createClient();


        // 获取响应对象
        SendSmsResponse sendSmsResponse = null;
        try {
            sendSmsResponse = client.sendSms(sendSmsRequest);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //        if (sendSmsResponse.getStatusCode() != 200) {
//            throw new BusinessException("短信发送失败");
//        }
        // 响应包含服务端响应的 body 和 headers
        //sendSmsResponse.getBody().getCode()
        System.out.println(toJSONString(sendSmsResponse));
        return sendSmsResponse;

    }
}